package com.dongdongshop.controller;

import com.dongdongshop.data.PageResult;
import com.dongdongshop.data.Result;
import com.dongdongshop.domain.rep.SpecificationReq;
import com.dongdongshop.domain.res.SpecificationOptionRes;
import com.dongdongshop.service.SpecService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RefreshScope
@Api(tags = "东东商城规格管理")
@RequestMapping("spec")
public class SpecController {

    @Autowired
    private SpecService specService;

    /**
     * 规格分页查询
     * @param specificationReq
     * @return
     */
    @PostMapping("queryPage")
    @ApiOperation("规格分页查询")
    public Result<PageResult> queryPage(@RequestBody SpecificationReq specificationReq) {
        PageResult pageResult=specService.queryPage(specificationReq);
        return Result.success(pageResult);
    }

    /**
     * 通过specId查询Option所有数据
     * @param id
     * @return
     */
    @PostMapping("queryOptionBySpecId/{id}")
    @ApiOperation("通过specId查询Option所有数据")
    public Result<List<SpecificationOptionRes>> queryOptionBySpecId(@PathVariable("id") Long id) {
        List<SpecificationOptionRes> resList=specService.queryOptionBySpecId(id);
        return Result.success(resList);
    }


    /**
     * 规格增加修改操作
     * @param specificationReq
     * @return
     */
    @PutMapping("save")
    @ApiOperation("规格增加修改操作")
    public Result save(@RequestBody SpecificationReq specificationReq) {
        specService.save(specificationReq);
        return Result.success();
    }


    /**
     * 规格删除操作
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    @ApiOperation("规格删除操作")
    public Result delete(@PathVariable("id") Long id){
        specService.delete(id);
        return Result.success();
    }


    /**
     * 规格批量删除操作
     * @param ids
     * @return
     */
    @PatchMapping("batchRemove")
    @ApiOperation("规格批量删除操作")
    public Result batchRemove(@RequestBody List<Integer> ids){
        specService.batchRemove(ids);
        return Result.success();
    }
}
